from flask_demo import app_database


class User(app_database.Model):
    user_id = app_database.Column(app_database.Integer, primary_key=True)
    username = app_database.Column(app_database.String(80), unique=True, nullable=False)
    password = app_database.Column(app_database.String(80), nullable=False)
    role = app_database.Column(app_database.Integer, nullable=False)

    def __init__(self, username, password, role):
        self.username = username
        self.password = password
        self.role = role
        pass

    def __repr__(self):
        return '<User user_id=%r username=%r role=%r>' % (self.user_id, self.username, self.role)

    def to_dict(self):
        return {
            "user_id": self.user_id,
            "username": self.username,
            "role": self.role
        }

    pass


class Component(app_database.Model):
    id = app_database.Column(app_database.Integer, primary_key=True)
    content = app_database.Column(app_database.Text)
    # 指定外键关联的属性
    author_id = app_database.Column(app_database.Integer, app_database.ForeignKey('user.user_id'))
    # 设计关系，并没有实际的字段
    author = app_database.relationship(
        "User",
        backref=app_database.backref("components", lazy='dynamic')
    )

    def __init__(self, content, author_id):
        self.content = content
        self.author_id = author_id
        pass

    def __repr__(self):
        return '<Component %r>' % self.id


class ImgComponent(app_database.Model):

    img_component_id = app_database.Column(app_database.Integer, primary_key=True)
    author_id = app_database.Column(app_database.Integer, app_database.ForeignKey('user.user_id'))
    # relationship() 并不会创建新字段，只在 Python 中使用的时候有用
    author = app_database.relationship(
        "User",
        backref=app_database.backref("img_components", lazy='dynamic')
    )
    title = app_database.Column(app_database.String(30), nullable=False)
    img_url = app_database.Column(app_database.Text, nullable=False)
    group_name = app_database.Column(app_database.String(30), nullable=False)
    public = app_database.Column(app_database.Integer, nullable=False)
    text_str = app_database.Column(app_database.String(30), nullable=False)

    def __init__(self, author_id: int, title: str, img_url: str, group_name: str, public: int, text_str: str):
        self.author_id = author_id
        self.title = title
        self.img_url = img_url
        self.group_name = group_name
        self.public = public
        self.text_str = text_str
        pass

    def __repr__(self):
        return '<ImgComponent img_component_id=%r>' % self.img_component_id

    # @staticmethod
    def to_dict(self):
        return {
            "img_component_id": self.img_component_id,
            "img_url": self.img_url,
            "author_id": self.author_id,
            "title": self.title,
            "group_name": self.group_name,
            "text_str": self.text_str,
            "public": self.public
        }
    pass


class LogicComponent(app_database.Model):
    logic_component_id = app_database.Column(app_database.Integer, primary_key=True)
    author_id = app_database.Column(app_database.Integer, app_database.ForeignKey('user.user_id'))
    # relationship() 并不会创建新字段，只在 Python 中使用的时候有用
    author = app_database.relationship(
        "User",
        backref=app_database.backref("logic_components", lazy='dynamic')
    )
    title = app_database.Column(app_database.String(30), nullable=False)
    img_url = app_database.Column(app_database.String(100), nullable=False)
    group_name = app_database.Column(app_database.String(30), nullable=False)
    type_int = app_database.Column(app_database.Integer, nullable=False)
    middle_parameter = app_database.Column(app_database.Text)
    input_parameter = app_database.Column(app_database.Text)
    output_parameter = app_database.Column(app_database.Text)
    public = app_database.Column(app_database.Integer, nullable=False)
    text_str = app_database.Column(app_database.String(30), nullable=False)
    file_path = app_database.Column(app_database.String(100), nullable=False)

    def __init__(
            self,
            author_id: int,
            title: str,
            img_url: str,
            group_name: str,
            public: int,
            text_str: str,
            file_path: str,
            type_int: int,
            middle_parameter: str,
            input_parameter: str,
            output_parameter: str,
    ):
        self.author_id = author_id
        self.title = title
        self.img_url = img_url
        self.group_name = group_name
        self.public = public
        self.text_str = text_str
        self.file_path = file_path
        self.type_int = type_int
        self.middle_parameter = middle_parameter
        self.input_parameter = input_parameter
        self.output_parameter = output_parameter
        pass

    def __repr__(self):
        return '<LogicComponent logic_component_id=%r>' % self.logic_component_id

    def to_dict1(self):
        return {
            "logic_component_id": self.logic_component_id,
            "author_id": self.author_id,
            "author": self.author.to_dict() if self.author is not None else None,
            "title": self.title,
            "img_url": self.img_url,
            "group_name": self.group_name,
            "public": self.public,
            "text_str": self.text_str,
            "file_path": self.file_path,
            "input_parameter": self.input_parameter,
            "middle_parameter": self.middle_parameter,
            "output_parameter": self.output_parameter,
        }

    pass


class ImgWork(app_database.Model):
    img_work_id = app_database.Column(app_database.Integer, primary_key=True)
    title = app_database.Column(app_database.String(50), nullable=False)
    author_id = app_database.Column(app_database.Integer, app_database.ForeignKey('user.user_id'))
    # relationship() 并不会创建新字段，只在 Python 中使用的时候有用
    author = app_database.relationship(
        "User",
        backref=app_database.backref("img_works", lazy='dynamic')
    )
    published = app_database.Column(app_database.Integer, nullable=False)
    content = app_database.Column(app_database.Text)
    database_host = app_database.Column(app_database.String(50))
    database_name = app_database.Column(app_database.String(50))
    database_username = app_database.Column(app_database.String(50))
    database_password = app_database.Column(app_database.String(50))

    def __init__(self, author_id: int, published: int, content: str, database_host: str, database_name: str, database_username: str, database_password: str, title: str):
        self.author_id = author_id if author_id is not None else -1
        self.published = published if published is not None else 0
        self.content = content
        self.database_host = database_host
        self.database_name = database_name
        self.database_username = database_username
        self.database_password = database_password
        self.title = title if title is not None else "未命名"
        pass

    def __repr__(self):
        return '<ImgWork img_work_id=%r>' % self.img_work_id

    def to_dict(self):
        return {
            "img_work_id": self.img_work_id,
            "author_id": self.author_id,
            "published": self.published,
            "title": self.title,
            "content": self.content,
            "database_name": self.database_name,
            "database_host": self.database_host,
            "database_username": self.database_username,
            "database_password": self.database_password,
        }

    def to_dict1(self):
        return {
            "img_work_id": self.img_work_id,
            "author": self.author.to_dict() if self.author is not None else None,
            "author_id": self.author_id,
            "title": self.title,
            "published": self.published,
            "database_name": self.database_name,
            "database_host": self.database_host,
        }
    pass


class LogicWork(app_database.Model):
    logic_work_id = app_database.Column(app_database.Integer, primary_key=True)
    author_id = app_database.Column(app_database.Integer, app_database.ForeignKey('user.user_id'))
    # relationship() 并不会创建新字段，只在 Python 中使用的时候有用
    author = app_database.relationship(
        "User",
        backref=app_database.backref("logic_works", lazy='dynamic')
    )
    content = app_database.Column(app_database.Text)
    title = app_database.Column(app_database.String(30))

    def __init__(self, author_id: int, content: str, title: str):
        self.author_id = author_id
        self.content = content
        self.title = title
        pass

    def __repr__(self):
        return '<LogicWork logic_work_id=%r>' % self.logic_work_id

    pass

